/*
 *  main.c
 *  pr1
 *
 *  Created by Javier Quevedo on 11/2/08.
 *  Copyright 2008 __MyCompanyName__. All rights reserved.
 *
 */




#include <stdio.h>
#if defined(WIN32)
#include <malloc.h>
#endif
#if defined(WIN32)
#include <GL/glut.h>
#else
#include <GLUT/glut.h>
#endif
#import "eventHandlers.h"
#import "glOperations.h"
#import "glutil.h"
#import "Model.h"
#import "mathOperations.h"
#import "Node.h"
#import "Dino.h"
#import "Camera.h"
#import "RunRunGame.h"

#define maxSphereX 0.3
#define maxSphereY 0.1

int shadingType = 0; // 0 = flat, 1 = smooth;
float lightposition[] = { 0.0, 6.0, 0.0, 1.0};
Camera *camera;
RunRunGame *game;


enum {
	MENU_TOGGLE_CUBE,
	MENU_TOGGLE_SHADING,
	MENU_INCREASE_SPEED,
	MENU_DECREASE_SPEED,
	MENU_EXIT
};




void setLight(void)
{
	GLfloat ambientLight[] = { 0.2f, 0.2f, 0.2f, 1.0f };
	GLfloat diffuseLight[] = { 0.8f, 0.8f, 0.8, 1.0f };
	GLfloat specularLight[] = { 0.5f, 0.5f, 0.5f, 1.0f };
	GLfloat position[] = { -1.5f, 1.0f, -4.0f, 1.0f };
	glEnable( GL_NORMALIZE );
	glLightModeli( GL_LIGHT_MODEL_LOCAL_VIEWER, 1);
	glEnable( GL_LIGHTING);
	glLightfv( GL_LIGHT1, GL_SPECULAR, specularLight);
	glLightfv( GL_LIGHT1, GL_AMBIENT, ambientLight);
	glLightfv( GL_LIGHT1, GL_DIFFUSE, diffuseLight);
	glLightfv( GL_LIGHT1, GL_POSITION, lightposition);
	glEnable( GL_LIGHT1);
	glEnable( GL_COLOR_MATERIAL);
	glEnable(GL_DEPTH_TEST);
	if (shadingType)
		glShadeModel(GL_SMOOTH);
	else 
		glShadeModel(GL_FLAT);	
	
}

void display(void)
{
	glClearColor(0.0, 0.0, 0.2, 0.3); //darkblue
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	placeCameraAt(camera);
	
	drawAxis();
	//drawGround();
	
	renderGame(game);
	glutSwapBuffers();
}


void toggleShading(void)
{
	printf("Toggling shading FLAT-SMOOTH\n");
	if (shadingType)
		shadingType = 0;
	else
		shadingType = 1;
	
	setLight();
}
void keyboardEventHandler(GLubyte key, GLint x, GLint y)
{        
	switch (key)
	{                                                         
		case 27: // scape key
		{
			//printf("lol\n");
			exit(0);
			break;
		}
		case 113: // q key
		{
			exit(0);
			break;
		}
		case 32: // space key
		{
			//toggleAnimation();
			
			break;
		}
		case 'h':
		{
			toggleShading();
			break;
		}
		case 'w':
			moveUp(camera, 1.0);
			break;
		case 's':
			//moveCamera(0,-1);
			moveDown(camera, 0.1);
			break;
		default:
		{
			gameKeyboardEventHandler(game, key);
			printf("key pressed: %c\n",key);
			break;
		}
			/* Default key handling code goes here */
	}
	
	//glError();
	
	glutPostRedisplay();        
}


/*
void loadTexture (const char *imageFile)
{
	groundImage=LoadRGB(imageFile);
	glGenTextures(1, &groundTexture);
	glBindTexture(GL_TEXTURE_2D, groundTexture);
	glTexImage2D(GL_TEXTURE_2D, 0, groundImage->components, groundImage->sizeX, 
				 groundImage->sizeY, 0, groundImage->format, GL_UNSIGNED_BYTE, groundImage->data); 
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); 
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT); 
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	
}*/


int createMenu(void)
{
/*	int menu;
	//menu = glutCreateMenu (SelectFromMenu);
	glutAddMenuEntry ("Toggle cube\tc", MENU_TOGGLE_CUBE);
	glutAddMenuEntry ("Toggle shading\th", MENU_TOGGLE_SHADING);
	glutAddMenuEntry ("Increase animation speed\t+", MENU_INCREASE_SPEED);
	glutAddMenuEntry ("Decrease animation speed\t-", MENU_DECREASE_SPEED);
	glutAddMenuEntry ("Exit\tEsc", MENU_EXIT);
	return menu;*/
}

void idle(void)
{
	updateGame(game, glutGet( GLUT_ELAPSED_TIME));
	glutPostRedisplay();
	
}




void mouseEventHandler(int button, int state, int x, int y)
{
	// Respond to mouse button presses.
	// If button1 pressed, mark this state so we know in motion function.
	if (button == GLUT_LEFT_BUTTON)
    {
		if (state == 0)
		{
			printf("left button pressed\n");
			
		}
		if (state ==1)
		{
			printf("left button released\n");
		}
    }
	if (button == GLUT_RIGHT_BUTTON)
    {
		createMenu();
		//printf("right button pressed\n");
    }
}



int main(int argc, char **argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_MULTISAMPLE);
	glutInitWindowSize(800, 600);
	
	glutInitWindowPosition(0, 0);
    glutCreateWindow("Final Project");
	
	glutKeyboardFunc(keyboardEventHandler);
	glutMouseFunc (mouseEventHandler);
	
	
	glEnable (GL_DEPTH_TEST);
	
	glutReshapeFunc(reshapeEventHandler);
	//glutMotionFunc(motionEventHandler);
	//glutPassiveMotionFunc(passiveMotionEventHandler);
	
	
	// create menu
	createMenu();
	glutAttachMenu (GLUT_RIGHT_BUTTON);
	
	
	glutInitDisplayMode(GLUT_MULTISAMPLE);
	
	
	//glEnable( GL_TEXTURE_2D );
	
	//loadTexture("textures/birch.rgb");

	//truckModel.layerCount = 9;
	//truckModel.layers = malloc(sizeof(GLuint)*9);
	
	//dinoSaur = initDino(dinoSaur);
	
	/*dinoModel = malloc(sizeof(Model));
	dinoFiles = malloc(sizeof(char)*4*40);
	dinoFiles[0] ="models/dino/arm.sgf";
	dinoFiles[1] ="models/dino/body.sgf",
	dinoFiles[2] ="models/dino/eyes.sgf",
	dinoFiles[3] ="models/dino/leg.sgf";
	
	
	LoadModel(dinoModel, dinoFiles, 4);*/
	
	//truckModel = malloc(sizeof(Model));
	//LoadModel(truckModel, truckFiles, 9);
	
	
	
	
	

	
	camera = malloc(sizeof(Camera));	
	game = initRunRunGame(camera);
	//setCamera(camera, -1.0, 1.0, -1.0, -0.0, -0.0, 1.0);
	
	//eventsSetCamera(camera);
	setLight();
	//glutIdleFunc(idle);
	//glutDisplayFunc(display);

	glutSwapBuffers();
	glutMainLoop();
}